﻿using CDH_Shop.Models.Database;
using CDH_Shop.Models.Good;
//命名空间 在这个文件夹去找某些类的文件
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CDH_Shop.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GoodsController : ControllerBase
    {
        private readonly CDH_ShopContext _db;
        /// <summary>
        /// 构造函数 构造注入
        /// </summary>
        /// <param name="db"></param>
        public GoodsController(CDH_ShopContext db)
        {
            _db = db;
        }

        //后台商品管理（商品的新增，删除，修改，查询。新增或者修改商品使需输入商品名称、商品图片、商品价格、商品库存、商品描述、商品分类、商品状态（上架、下架））
        /// <summary>
        /// 新增商品
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string AddGood(AddGoodRequest request) //接口
        {
            //实现
            //类的转换
            var good = new Good
            {
                GoodName = request.GoodName,
                CateId = request.CateId,
                GoodCover = request.GoodCover,
                CreateTime = DateTime.Now,
                Description = request.Description,
                Price = request.Price,
                State = request.State,
                Stock = request.Stock
            };
            _db.Goods.Add(good);
            var row = _db.SaveChanges();
            if (row > 0)
                return "新增成功";
            return "新增失败";
        }
        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        [HttpPost]
        public string DeleteGood(int goodId)
        {
            //实现接口
            var good = _db.Goods.FirstOrDefault(x => x.GoodId == goodId);
            if (good == null)
                return "商品不存在";

            _db.Goods.Remove(good);
            var row = _db.SaveChanges();
            if (row > 0)
                return "删除成功";
            return "删除失败";
        }
        /// <summary>
        /// 修改商品
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public string UpdateGood(UpdateGoodRequest request)
        {
            //实现
            var good = _db.Goods.FirstOrDefault(x => x.GoodId == request.GoodId);
            if (good == null)
                return "商品不存在";

            //商品存在 
            //模型赋值
            //类的转换 即使他们有相同属性名字

            //企业开发里 工具 AotoMap 作用把两个里面有相同属性的类进行转换映射
            good.GoodName = request.GoodName;
            good.GoodCover = request.GoodCover;
            good.Price = request.Price;
            good.State = request.State;
            good.Stock = request.Stock;
            good.Description = request.Description;

            _db.Goods.Update(good);
            var row = _db.SaveChanges();
            if (row > 0)
                return "修改成功";
            return "修改失败";
        }

        //商品查询
        /// <summary>
        /// 查询商品
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public List<Good> GetGoods(GetGoodsRequest request)
        {
            //关键字
            var goodDb = _db.Goods.AsQueryable();
            //如果输入关键字 则添加这个条件
            if (!string.IsNullOrEmpty(request.Keyword))
                goodDb = goodDb.Where(x => x.GoodName.Contains(request.Keyword));
            //否则不添加条件

            //分页
            return goodDb
                  .OrderByDescending(x => x.GoodId)
                  //跳过
                  .Skip((request.PageIndex - 1) * request.PageSize)
                  //取多少个
                  .Take(request.PageSize)
                  //ToList()
                  //之前是编辑SQL
                  //之后执行SQL
                  //执行查询语句
                  .ToList();
        }

        /// <summary>
        /// 添加商品分类
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string AddCategory(AddCategoryRquest request)
        {
            var category = new Category
            {
                CateName = request.CateName
            };


            _db.Categories.Add(category);
            var row = _db.SaveChanges();
            if (row > 0)
                return "新增成功";
            return "新增失败";
        }
        /// <summary>
        /// 删除商品分类
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string DeleteCategroy(int cateId)
        {
            var category = _db.Categories.FirstOrDefault(x => x.CateId == cateId);
            if (category == null)
                return "分类不存在";

            _db.Categories.Remove(category);
            var row = _db.SaveChanges();
            if (row > 0)
                return "删除成功";
            return "删除失败";
        }
        /// <summary>
        /// 更新商品分类
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public string UpdateGategory(UpdateGategoryRequest request)
        {
            var category = _db.Categories.FirstOrDefault(x => x.CateId == request.CateId);
            if (category == null)
                return "分类不存在";

            category.CateName = request.CateName;
            _db.Categories.Update(category);
            var row = _db.SaveChanges();
            if (row > 0)
                return "更新成功";
            return "更新失败";
        }
        /// <summary>
        /// 获取商品分类列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public List<Category> GetCategories(GetCategoriesRequest request)
        {
            //待执行命令 正在组建SQL语句
            var categoryDB = _db.Categories.AsQueryable();
            if (!string.IsNullOrEmpty(request.Keyword))
                categoryDB = categoryDB.Where(x => x.CateName.Contains(request.Keyword));

            //返回查询结果
            return categoryDB
                .OrderByDescending(x => x.CateId)
                .Skip((request.PageIndex - 1) * request.PageSize)
                .Take(request.PageSize)
                .ToList();
        }
    }
}
